package com.sufutian.ffmpegdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sufutian.jnidemo.JNIDemo;

public class MainActivity extends AppCompatActivity {

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e("sufutian", ": 有存储权限");
            } else {
                Log.e("sufutian", ": 没有有存储权限");
            }
        }
    }

    private int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 110;


    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
        }

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                run();
                JNIDemo jniDemo = new JNIDemo();
                int runCommand = jniDemo.runCommand(10);
                Log.e("sufutian==", "onClick: " + runCommand);
            }
        });


    }


    public void run() {
        String base = Environment.getExternalStorageDirectory().getPath();
        Log.e("PATH", base);
        String[] commands = new String[9];
        commands[0] = "ffmpeg";
        commands[1] = "-i";
        commands[2] = base + "/input.mp4";
        commands[3] = "-i";
        commands[4] = base + "/input.wav";
        commands[5] = "-strict";
        commands[6] = "-2";
        commands[7] = "-y";
        commands[8] = base + "/merge.mp4";
        int result = FFMpegUtil.runCommand(commands);
        if (result == 0) {
            Log.e("sufutian==", "run: 完成");
            Toast.makeText(MainActivity.this, "命令行执行完成", Toast.LENGTH_SHORT).show();
        } else {
            Log.e("sufutian==", "run: 失败");
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
